﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Windows.Forms;

namespace SCMS
{
    public class ItemsDAO : DAO<ItemsInfo>
    {
        public ItemsDAO(DAOManager aDAOManager)
        {
            base.mDAOManager = aDAOManager;
            base.mInfo = new ItemsInfo();
        }

        public override bool Save()
        {
            string l_query = "Select syskey, itemID, itemName, itemPrice, itemDescription, itemQuantity, supplierID FROM Inventory WHERE syskey=" + this.Info.syskey;

            try
            {
                return SaveDAO(l_query, "Inventory");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public override bool Delete()
        {
            string l_query = "DELETE FROM Inventory WHERE syskey=" + this.Info.syskey;

            try
            {
                return DeleteDAO(l_query, "Inventory");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }

        public override ItemsInfo Find()
        {
            string l_query = "SELECT syskey, itemID, itemName, itemPrice, itemDescription, itemQuantity, supplierID FROM Inventory WHERE itemID=" + this.Info.itemID;

            try
            {
                return FindDAO(l_query);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        protected override void UpdateRecord(ref DataRow aDataRow, ref ItemsInfo aInfo)
        {
            try
            {
                if (aInfo == null)
                {
                    aInfo = new ItemsInfo();
                }

                aDataRow["syskey"] = aInfo.syskey;
                aDataRow["itemID"] = aInfo.itemID;
                aDataRow["itemName"] = aInfo.itemName;
                aDataRow["itemPrice"] = aInfo.itemPrice;
                aDataRow["itemDescription"] = aInfo.itemDescription;
                aDataRow["itemQuantity"] = aInfo.itemQuantity;
                aDataRow["supplierID"] = aInfo.supplierID;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        protected override void ReadRecord(ref IDataReader aDataReader, ref ItemsInfo aInfo)
        {
            try
            {
                if (aInfo == null)
                {
                    aInfo = new ItemsInfo();
                }
                aInfo.syskey = GetInt64(aDataReader, "syskey");
                aInfo.itemID = GetString(aDataReader, "itemID");
                aInfo.itemName = GetString(aDataReader, "itemName");
                aInfo.itemPrice = GetDouble(aDataReader, "itemPrice");
                aInfo.itemDescription = GetString(aDataReader, "Description");
                aInfo.itemQuantity = GetInt32(aDataReader, "itemQuantity");
                aInfo.supplierID = GetString(aDataReader, "SupplierID");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public override bool Saves(List<ItemsInfo> aList)
        {
            try
            {
                foreach (ItemsInfo l_info in aList)
                {
                    Info = l_info;
                    Save();
                }
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public override List<ItemsInfo> Finds()
        {
            string l_query = "";
            try
            {
                return FindsDAO(l_query);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public override bool SaveDataSet(DataSet aDataSet)
        {
            string l_insert_query = "INSERT INTO Inventory (syskey, itemID, itemName, itemPrice, itemDescription, itemQuantity, supplierID)" +
                "VALUES (" + this.Info.syskey + ",'" + this.Info.itemID + "','" + this.Info.itemName + "',"
                + this.Info.itemPrice + ",'" + this.Info.itemDescription + "'," + this.Info.itemQuantity + ",'" + this.Info.supplierID + "')";

            string l_update_query = "UPDATE ItemsControl SET " +
               "itemID= '" + this.Info.itemID + "'" +
               ", itemName='" + this.Info.itemName + "'" +
               ", itemPrice=" + this.Info.itemPrice +
               ", itemDescription='" + this.Info.itemDescription + "'" +
               ", itemQuantity= " + this.Info.itemQuantity +
               ", supplierID='" + this.Info.supplierID + "'" +
               "WHERE syskey=" + this.Info.syskey;

            string l_delete_query = "DELETE FROM Inventory WHERE syskey=" + this.Info.syskey;

            try
            {
                DbCommand l_insert_cmd = GetSqlStringCommand(l_insert_query);
                DbCommand l_update_cmd = GetSqlStringCommand(l_update_query);
                DbCommand l_delete_cmd = GetSqlStringCommand(l_delete_query);

                AddInParameter(l_insert_cmd, "syskey", DbType.Int64);
                AddInParameter(l_insert_cmd, "itemID", DbType.String);
                AddInParameter(l_insert_cmd, "itemName", DbType.String);
                AddInParameter(l_insert_cmd, "itemPrice", DbType.Double);
                AddInParameter(l_insert_cmd, "itemDescription", DbType.String);
                AddInParameter(l_insert_cmd, "itemQuantity", DbType.Int32);
                AddInParameter(l_insert_cmd, "supplierID", DbType.String);

                AddInParameter(l_update_cmd, "syskey", DbType.Int64);
                AddInParameter(l_update_cmd, "itemID", DbType.String);
                AddInParameter(l_update_cmd, "itemName", DbType.String);
                AddInParameter(l_update_cmd, "itemPrice", DbType.Double);
                AddInParameter(l_update_cmd, "itemDescription", DbType.String);
                AddInParameter(l_update_cmd, "itemQuantity", DbType.Int32);
                AddInParameter(l_update_cmd, "supplierID", DbType.String);

                AddInParameter(l_delete_cmd, "syskey", DbType.Int64);

                UpdateDataSet(aDataSet, "Inventory", l_insert_cmd, l_update_cmd, l_delete_cmd);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw e;
            }
        }

        public override DataSet GetDataSet()
        {
            throw new NotImplementedException();
        }
    }
}
